<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>

<script>
    /* 声明格式：let reg = /pattern/[flags(i忽略大小写/g全文匹配/m多行匹配)] */
    let formatReg1 = /cat/;
    let formatReg2 = /cat/gi;

    let str = 'User13UseraaaUser345';

    /* replace(reg,str)：根据正则替换字符串，flag存在g则全文匹配 */
    console.log(str.replace(/User\d/, '--'));//--3UseraaaUser345
    console.log(str.replace(/User\d/g, '--'));//--3Useraaa--45
    console.log(str.replace(/user\d/gi, '--'));//--3Useraaa--45

    /* split(reg)：根据正则截取字符串，无论flag存不存在g都全文匹配 */
    console.log(str.split(/User\d/));//['', '3Useraaa', '45']
    console.log(str.split(/User\d/g));//['', '3Useraaa', '45']
    console.log(str.split(/user\d/gi));//['', '3Useraaa', '45']

    /* match(reg)：根据正则返回匹配字符串数组，flag存在g则全文匹配 */
    console.log(str.match(/User\d/));//['User1']
    console.log(str.match(/User\d/g));//['User1', 'User3']
    console.log(str.match(/user\d/gi));//['User1', 'User3']

    /* search(reg)：根据正则返回匹配字符串索引，无论flag存不存在g都不会全文匹配 */
    console.log(str.search(/User\d/));//0
    console.log(str.search(/User\d/g));//0
    console.log(str.search(/user\d/gi));//0
</script>
</html>